import Axios from "axios";
Axios.defaults.baseURL="http://localhost:8647/";

/**
 *
 * @param config {url:"地址",data:{"参数key","参数value"}}
 * @returns {Promise<AxiosResponse<T>>}
 */

let get =  function get(url, data, headers) {
    //1.创建axios的实例,配置实例的全局配置
    let config = new Object();
    config.url = url
    config.methods = 'GET'
    config.params = data
    let header = headers;
    if (header) {
        header["Access-Control-Allow-Origin"]="*";
    }else {
        header = new Object();
        header["Access-Control-Allow-Origin"]="*";
    }
    config.headers = header
    let instance= Axios.create();
    return instance(config);
}

let post = async function post(config) {
    //1.创建axios的实例,配置实例的全局配置
    config.methods='POST'
    const instance= Axios.create()
    return  instance(config);
}

let postJson =function postJson(data,headers) {
    let config = new Object();
    if (headers) {
        headers.put("Content-Type","application/json;charset=utf-8")
    }else {
        config.headers={"Content-Type":"application/json;charset=utf-8"}
    }
    config.methods='POST'
    config.data=JSON.stringify(data)
    const instance= Axios.create()
    return  instance(config);
}

let queryIP =  function (_ip1,_ip2,_ip3,_ip4,url) {
    let ip = _ip1+"."+_ip2+"."+_ip3+"."+_ip4;
    console.log("IP:"+ip);
    Axios.defaults.baseURL="http://"+ip+":9090";
    return get(url);
}
export {get,post,postJson,queryIP};